package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/param")
public class ParamController {
    // http://localhost:8080/param/test1?id=1&name=zzz&gender=false&date=2018-12-12 12:20:30
    //注意如果是日期类型需要用分隔线分隔年月日，直接用Date来收参
    @RequestMapping("/test1")
    public String test1(Integer id, String name, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date date){
        System.out.println(id);
        System.out.println(name);
        System.out.println(date);

        return "/hi.html";


    }

    // http://localhost:8080/param/test2?id=1&name=zzz&gender=false&date=2018-12-12 12:20:30
    @RequestMapping("/test2")
    public String test2(User user){
        System.out.println(user);
        return "/hi.html";
    }

    /**
     * 数组收参
     * @param hobby
     * @return
     */
    @RequestMapping("/test3")
    public String test3(String[] hobby){
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/hi.html";
    }

    @RequestMapping("/test4")
    public String test4(User user){
        for (String s : user.getHobby()) {
            System.out.println(s);
        }
        return "/hi.html";
    }

    /**
     * 多级路径收参
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/test5/{name}/{password}")
    public String test5(@PathVariable("name") String name,@PathVariable("password") String password){
        System.out.println(name);
        System.out.println(password);
        return "/hi.html";
    }

    @RequestMapping("/test6/{name}/user/{password}")
    public String test6(@PathVariable("name") String name,@PathVariable("password") String password){
        System.out.println(name);
        System.out.println(password);
        return "/hi.html";
    }
}
